var Eg_url = sysConfig.urlConfig.eg_url;
var Wz_url = sysConfig.urlConfig.wz_url;
var tokenStr = "";
var deleteWzs = new Array();
var originalData = "";//记录初始数据
$(function(){
     sjGMtable($('input[name=GMtable]').val());
     if (window.android != null && typeof (window.android) != "undefined") {
        tokenStr = window.android.callAndroidCurtUserToken();//获取当前token值
     }
});
//执行页面初始化加载数据
function callJsWebViewInitComplete(){
   if (window.android != null && typeof (window.android) != "undefined") {
      window.android.callAndroidWebViewInitComplete();
   }else {
      alert(typeof (window.android));
   }
}
// 物种输入查询 --- 模糊查询
function ZWdataYFone(i) {
    baidusearchinput["control_init"]($("input#text" + i + ""), $("input#hiddentext" + i + ""));
    baidusearchinput["control_init"]($("input#CBWZMC" + i + ""), $("input#CBWZDM" + i + ""));
    baidusearchinput["control_init"]($("input#GMWZMC" + i + ""), $("input#GMWZDM" + i + ""));
}
// 灌木的循环添加表单 GMloopTableshow
function sjGMtable(GMyfID) {
    let html = "";
    for (var i = 1; i <= GMyfID; i++) {
        let GMtrtd = '<td><input class="WZGD td_input" type="text" onkeyup="clearNoNum(this)"></input></td>'
            + '<td>'
            + '<span><input class="CFLENGTH td_input" type="text" onkeyup="clearNoNum(this)"></input></span>'
            + '<span><input class="CFWIDTH td_input" type="text" onkeyup="clearNoNum(this)"></input></span>'
            + '</td>'
            + '<td><input class="ZCS td_input" type="text" onkeyup="clearNoNum(this)"></input></td>'
            + '<td onclick="WZdelete(' + i + ')">删除</td>';
        html += '<tr id="GMtr' + i + '"><td class="WZMC" style="position: relative;"><i class="fa fa-times" aria-hidden="true"><input name="SHBZVAL" class="SHBZVAL-YFMJ SHBZ-GM-WZ SHBZ-GM-WZ-' + (i - 1) + '" type="hidden" value="0"/></i><input id="GMWZMC' + i + '" name="wzmc" type="text" onkeydown="ZWdataYFone(\'' + i + '\')" placeholder="输入植物名称" style="width: 89%; outline: none; background: #6CBCFE; border: 0"><input type="hidden" id="GMWZDM' + i + '" name="wzdm" ><input id="GMYFid' + i + '" type="hidden" name="GMYFid" value="GMYFid"><input type="hidden" id="GMWZID' + i + '" name="GMwzid" value="0" /><input type="hidden" id="DBMARK'+i+'" value="0"/></td>' + GMtrtd + '</tr>';
    }
    $("#form_GMidone .table_tab tbody").html(html);
    // 点击增加灌木样方表单 GMaddQuadratI
    $(".body .diplay4 .table_Shrub .table_tab .add_button").hover(function () {
        $(this).css("background", "#0271BC");
        $(this).css("cursor", "pointer");
    }, function () {
        $(this).css("background", "");
        $(this).css("cursor", "");
    });

    $(".body .diplay4 .table_Shrub .table_tab .add_button").click(function () {
        let number = parseInt($('input[name=GMtable]').val()) + 1;
        let GMtrtd = '<td><input class="WZGD td_input" type="text" onkeyup="clearNoNum(this)"></input></td>'
            + '<td>'
            + '<span><input class="CFLENGTH td_input" type="text" onkeyup="clearNoNum(this)"></input></span>'
            + '<span><input class="CFWIDTH td_input" type="text" onkeyup="clearNoNum(this)"></input></span>'
            + '</td>'
            + '<td><input class="ZCS td_input" type="text" onkeyup="clearNoNum(this)"></input></td>'
            + '<td onclick="WZdelete(' + number +')">删除</td>';
        // var GMyfID = $(".body .diplay4 .table_Shrub .table_tab tbody tr").length;
        // var number = GMyfID + 1;
        $('input[name=GMtable]').val(number);
        let html = "";
        html += '<tr id="GMtr' + number + '"><td class="WZMC" style="position: relative;"><i class="fa fa-times" aria-hidden="true"><input name="SHBZVAL" class="SHBZVAL-YFMJ SHBZ-GM-WZ SHBZ-GM-WZ-' + number + '" type="hidden" value="0"/></i><input id="GMWZMC' + number + '" name="wzmc" type="text" onkeydown="ZWdataYFone(\'' + number + '\')" placeholder="输入植物名称" style="width: 89%; outline: none; background: #6CBCFE; border: 0"><input type="hidden" id="GMWZDM' + number + '" name="wzdm" ><input id="GMYFid' + number + '" type="hidden" name="GMYFid" value="GMYFid"><input type="hidden" id="GMWZID' + number + '" name="GMwzid" value="0" /><input type="hidden" id="DBMARK'+number+'" value="1"/></td>' + GMtrtd + '</tr>';
        // html += '<tr id="GMtr' + number + '">' + GMtrtd  +'</tr>';
        $("#form_GMidone .table_tab tbody").append(html);
    });
}


//隐藏保存按钮
function callJsError() {
    $('.XZ_button_bc').remove('input');
}
// 获取当前年份
function getCurtYear(){
    let mydate = new Date();
    let curtYear = mydate.getFullYear();
    return curtYear;
}
//
function callJsInitGmYfInfo(data) {
     let yfCode = data.yfCode;
     if(yfCode.split("-").length == 1){
        $('#GMoneNameC').html(data.ydName);//样地名称
        $("#GMoneDm").val(data.ydCode);//样地代码
        $('#GMoneYFdataC').html(data.yfName.substring(data.yfName.lastIndexOf("方")+1));//样方编号
        $("#GMoneYFmjC").val("");//样方面积
        $("#GMoneYFzgdC").val("");//总覆盖率
        sjGMtable($('input[name=GMtable]').val());
     }else{
        gmYfQuery(yfCode);
     }
};
//只能输入数字和小数点
function clearNoNum(obj)
{
    //先把非数字的都替换掉，除了数字和.
    obj.value = obj.value.replace(/[^\d.]/g,"");
    //必须保证第一个为数字而不是.
    obj.value = obj.value.replace(/^\./g,"");
    //保证只有出现一个.而没有多个.
    obj.value = obj.value.replace(/\.{2,}/g,".");
    //保证.只出现一次，而不能出现两次以上
    obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
}
function WZdelete(id){
    $("#form_GMidone .table_tab #GMtr" + id + " td #DBMARK" + id + "").val(3);
    let str = JSON.stringify({
        SHBZ: $("input[name=GMWZSHBZ]").val(),
        SHRUBID: $("#GMoneYFdataC").text(),
        WZDM: $("#form_GMidone .table_tab #GMtr" + id + " td #GMWZDM" + id + "").val(),
        WZMC: $("#form_GMidone .table_tab #GMtr" + id + " td #GMWZMC" + id + "").val(),
        WZGD: $("#form_GMidone .table_tab #GMtr" + id + " td>input.WZGD").val(),
        CFLENGTH: $("#form_GMidone .table_tab #GMtr" + id + " td>span>input.CFLENGTH").val(),
        CFWIDTH: $("#form_GMidone .table_tab #GMtr" + id + " td>span>input.CFWIDTH").val(),
        ZCS: $("#form_GMidone .table_tab #GMtr" + id + " td>input.ZCS").text(),
        DBMARK:$("#form_GMidone .table_tab #GMtr" + id + " td #DBMARK" + id + "").val()
    });
    if ($('#GMWZMC' + id).val() != '') {
        if (window.android != null && typeof (window.android) != "undefined") {
            window.android.callAndroidRemoveShowDialog(str, id);
        } else {
            alert(typeof (window.android));
        }
    }
}
function callJsRemoveWzInfo(deleteWzData,id) {
    $('#GMtr' + id).remove('tr');
    deleteWzs.push(deleteWzData);
    //alert('删除成功');
}
//获取提交页面数据
function getTabDataStr(){
    checkWzInfoElementHtml();
    let wzInfos = new Array();
    let GMyfID= $('input[name=GMtable]').val();
    let ID = -1;
    for (var i = 1; i <= GMyfID; i++) {
        if ($("#form_GMidone .table_tab tbody #GMtr" + i + ">td.WZMC>input[type='text']").val() != null && $("#form_GMidone .table_tab tbody #GMtr" + i + ">td.WZMC>input[type='text']").val() != "") {
            ID = ID + 1;
            wzInfos[ID] = {
                CFLENGTH: $("#form_GMidone .table_tab #GMtr" + i + " td>span>input.CFLENGTH").val(),
                CFWIDTH: $("#form_GMidone .table_tab #GMtr" + i + " td>span>input.CFWIDTH").val(),
                WZDM: $("#form_GMidone .table_tab #GMtr" + i + " td #GMWZDM" + i + "").val(),
                WZMC: $("#form_GMidone .table_tab #GMtr" + i + " td #GMWZMC" + i + "").val(),
                ZCS: $("#form_GMidone .table_tab #GMtr" + i + " td>input.ZCS").val(),
                SHBZ: $("input[name=GMWZSHBZ]").val(),
                SHRUBID: $("#GMoneYFdataC").text(),
                WZGD: $("#form_GMidone .table_tab #GMtr" + i + " td>input.WZGD").val(),
                DBMARK:$("#form_GMidone .table_tab #GMtr" + i + " td #DBMARK" + i + "").val()
            }
        }
    }
    let _shrub_wz = wzInfos.concat(deleteWzs);
    deleteWzs.length = 0;//清空被删除数组
    let _sampleplotid =$("#GMoneDm").val();
    //window.android.callAndroidShowLog("sampleplotid------------->"+_sampleplotid+"------"+_sampleplotid.split("-").length);
    if(_sampleplotid.split("-").length == 2){
        _sampleplotid = _sampleplotid+"-"+getCurtYear();
    }
    return JSON.stringify({
            SAMPLEPLOTID: _sampleplotid,
            SAMPLEPLOTNAME: $("#GMoneNameC").text(),
            SHRUBID: $("#GMoneYFdataC").text(),//样方号
            YFMJ: $("#GMoneYFmjC").val(),
            ZGD: $("#GMoneYFzgdC").val(),
            SHBZ: $("input[name=GMXXSHBZ]").val(),
            SHRUB_WZ: _shrub_wz
    });
};

//保存数据到本地
function SaveDataToLocal(){
    if($("#GMoneNameC").html() == ""){
        alert("请选择一个样方");
        return;
    }
    let dataStr = getTabDataStr();
    if (window.android != null && typeof (window.android) != "undefined") {
        window.android.callAndroidSaveShowDialog(dataStr);
    } else {
        alert(typeof (window.android));
    }
}
//提交数据到服务器
function CommitDataToServer(){
    if($("#GMoneNameC").html() == ""){
        alert("请选择一个样方");
        return;
    }
    //检查物种名称输入是否合法
    let isLegal = true;
    $("#form_GMidone .table_tab tr td.WZMC").each(function(){
        $(this).css({"border":"1px #ccc solid"});
        if($(this).find($("input[name='wzmc']")).val() != ""){
            if($(this).find($("input[name='wzdm']")).val() == ""){
                $(this).css({"border":"1px solid #FF0000"});
                isLegal = false;
                return;
            }
        }
    });
    if(!isLegal){
        alert("请选择正确的植物名称");
        return;
    }
    let dataStr = getTabDataStr();
    if (window.android != null && typeof (window.android) != "undefined") {
        window.android.callAndroidCommitShowDialog(dataStr);
    } else {
        alert(typeof (window.android));
    }
}
/*
*显示本地数据
*/
function callJsLocalExistGmYfInfo(data) {
   showGmyfTabFun(data);
}
function callJsGetCjDate() {
    if (window.android != null && typeof (window.android) != "undefined") {
        //window.android.callAndroidHtmlPageDateInfo($("#GMoneDataC").html());
    } else {
        alert(typeof (window.android));
    }
}
//将数据显示在页面中
function showGmyfTabFun(data){
    originalData = data;
    deleteWzs.length = 0;
    //if(data.SHRUB_WZ.length > $('input[name=GMtable]').val()){
        $('input[name=GMtable]').val(data.SHRUB_WZ.length);
        sjGMtable(data.SHRUB_WZ.length);
    //}
    // $("#GMYFBM").val(data.SHRUBID);//灌木编号
    $("input[name=GMXXSHBZ]").val(data.SHBZ);//灌木详细信息审核标志
    $("#GMoneNameC").html(data.SAMPLEPLOTNAME);//样地名称
    $("#GMoneDm").val(data.SAMPLEPLOTID);//样地代码
    $("#GMoneYFdataC").html(data.SHRUBID);//样方号
    $("#GMoneYFzgdC").val(data.ZGD);//总盖度
    $("#GMoneYFmjC").val(data.YFMJ);//样方面积
    for (let i = 0; i < data.SHRUB_WZ.length; i++) {
        let _dbmark = data.SHRUB_WZ[i].DBMARK;
        if(_dbmark == "" || _dbmark == null){
            _dbmark = 0;
        }
        if(_dbmark == 3){//不显示被删除的项
            break;
        }
        $("input[name=GMWZSHBZ]").val(data.SHRUB_WZ[i].SHBZ);//灌木详细信息审核标志
        $("#GMWZID" + (i + 1) + "").val(data.SHRUB_WZ[i].ID);
        $("#GMWZMC" + (i + 1) + "").val(data.SHRUB_WZ[i].WZMC);
        $("#GMWZDM" + (i + 1) + "").val(data.SHRUB_WZ[i].WZDM);
        $("#GMtr" + (i + 1) + " td input.WZGD").val(data.SHRUB_WZ[i].WZGD);
        $("#GMtr" + (i + 1) + " td span input.CFLENGTH").val(data.SHRUB_WZ[i].CFLENGTH);
        $("#GMtr" + (i + 1) + " td span input.CFWIDTH").val(data.SHRUB_WZ[i].CFWIDTH);
        $("#GMtr" + (i + 1) + " td input.ZCS").val(data.SHRUB_WZ[i].ZCS);
        $("#DBMARK" + (i + 1) + "").val(_dbmark);
    }
}
//检查物种信息是否有修改
function checkWzInfoElementHtml(){
    if(originalData != ""){
        let  GMyfID= $('input[name=GMtable]').val();
        for (let i = 1; i <= GMyfID; i++) {
            if ($("#form_GMidone .table_tab tbody #GMtr" + i + ">td.WZMC>input[type='text']").val() != null && $("#form_GMidone .table_tab tbody #GMtr" + i + ">td.WZMC>input[type='text']").val() != "") {
               let originalData_wzLens = originalData.SHRUB_WZ.length;
               for(let j = 0; j < originalData_wzLens; j++){
                    if($("#form_GMidone .table_tab #GMtr" + i + " td #GMWZMC" + i + "").val() == originalData.SHRUB_WZ[j].WZMC){
                        if($("#form_GMidone .table_tab #GMtr" + i + " td>input.WZGD").val() != originalData.SHRUB_WZ[j].WZGD){
                            $("#form_GMidone .table_tab #GMtr" + i + " td #DBMARK" + i + "").val(2);
                            break;
                        }else if($("#form_GMidone .table_tab #GMtr" + i + " td>span>input.CFLENGTH").val() != originalData.SHRUB_WZ[j].CFLENGTH){
                            $("#form_GMidone .table_tab #GMtr" + i + " td #DBMARK" + i + "").val(2);
                            break;
                        }else if($("#form_GMidone .table_tab #GMtr" + i + " td>span>input.CFWIDTH").val() != originalData.SHRUB_WZ[j].CFWIDTH){
                            $("#form_GMidone .table_tab #GMtr" + i + " td #DBMARK" + i + "").val(2);
                            break;
                        }else if($("#form_GMidone .table_tab #GMtr" + i + " td>input.ZCS").val() != originalData.SHRUB_WZ[j].ZCS){
                            $("#form_GMidone .table_tab #GMtr" + i + " td #DBMARK" + i + "").val(2);
                            break;
                        }else{
                            let tempDBMARK = originalData.SHRUB_WZ[j].DBMARK;
                            if(tempDBMARK == ""){
                              tempDBMARK = 0;
                            }
                            $("#form_GMidone .table_tab #GMtr" + i + " td #DBMARK" + i + "").val(tempDBMARK);
                        }
                        break;
                    }else{
                        $("#form_GMidone .table_tab #GMtr" + i + " td #DBMARK" + i + "").val(1);
                    }
               }
            }
        }
    }
}
//查询灌木样方信息
function gmYfQuery(yfCode){
    $.ajax({
        headers: {
           "Authorization":tokenStr
           },
        url:Eg_url+"ShrubQuadrat/"+yfCode,
        type: "get",
        dataType:"json",
        success: function (data) {
            window.android.callAndroidShowLog(JSON.stringify(data));
            showGmyfTabFun(data[0]);
        },
        error:function(data){
             window.android.callAndroidShowLog("-------cbYfQuery---error------"+JSON.stringify(data));
        }
    });
}